﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;
using medicine.DAL.DataSet1TableAdapters;
using System.Drawing;
using medicine.BaseComponents.Groups;
using medicine.General;

namespace medicine
{
    public partial class GroupsPanel : Panel, IDragSource, IDragDestination
    {
        
        

        public GroupsPanel()
        {
            InitializeComponent();
        }

        public GroupsPanel(IContainer container)
        {
            container.Add(this);

            InitializeComponent();
            OriginalColor = this.BackColor;
        }
      public void LoadGroupsFromDB()
        {
            MedicinUtilities.ClearGroupsLabels();
            DataTable dt = new GroupsTableAdapter().GetData();
            foreach (DataRow dr in dt.Rows)
            {
                GroupInfo CurrentGroupInfo = new GroupInfo();
                
                CurrentGroupInfo.DBDisplayField = dr["DisplayField"].ToString();
                CurrentGroupInfo.DBValueField = dr["ValueField"].ToString();
                CurrentGroupInfo.DBTableName = dr["TableName"].ToString();
                CurrentGroupInfo.DBValueFieldType = dr["ValueFieldType"].ToString();
                CurrentGroupInfo.Text = dr["GroupText"].ToString();
                CurrentGroupInfo.Order = (int)dr["Order"];
                AddGroup(dr["GroupText"].ToString(), CurrentGroupInfo);
            }
            this.Redraw();

        }
        public void AddGroup(string Text, GroupInfo CurrentGroupInfo)
        {
            GroupLabel MyLabel = new GroupLabel();

            MyLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                        | System.Windows.Forms.AnchorStyles.Right)));
            //MyLabel.BackColor = System.Drawing.SystemColors.ActiveCaption;            
            MyLabel.BackColor = Color.FromArgb(0, 167, 231); //; System.Drawing.Color.FromName("blue");
            MyLabel.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
            MyLabel.Font = new System.Drawing.Font("Tahoma", 9.75F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            MyLabel.Text = Text;
            MyLabel.Name = "label" + (this.Controls.Count + 1);
            MyLabel.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            MyLabel.Size = new System.Drawing.Size(this.ClientSize.Width - 2, 40);
            MyLabel.Location = new System.Drawing.Point(1, GetPanelControlsMaxTop(int.MaxValue) + 1);
            //MyLabel.MouseDown += new MouseEventHandler(label1_MouseDown);
            //MyLabel.MouseMove += new MouseEventHandler(MyLabel_MouseMove);
            //MyLabel.MouseUp += new MouseEventHandler(MyLabel_MouseUp);
            MyLabel.Tag = CurrentGroupInfo;
            MyLabel.GroupInfo  = CurrentGroupInfo;
            MyLabel.Click += new EventHandler(MyLabel_Click);
            //MyLabel.
            MyLabel.Mover.Dragging += new DraggingHandler(Mover_Dragging);
            MyLabel.Mover.DropTest += new DropTestHandler(Mover_DropTest);
            
            this.Controls.Add(MyLabel);
            MedicinUtilities.AddGroupsLabels(MyLabel);
            //MedicinUtilities.GroupsLabels.Add(MyLabel);
        }


        void MyLabel_Click(object sender, EventArgs e)
        {
            if (!((GroupLabel)sender).Mover.isMoved)
            {
                ItemSelectionFRM frm = new ItemSelectionFRM();
                if (DialogResult.OK == frm.ShowDialog(this, (GroupLabel)sender))
                {
                    ((GroupLabel)sender).SetColor();
                    ((GroupLabel)sender).FireAfterRowSelection();
                }
                    
            }
            //((GroupLabel)sender).Mover.isMoved = false;
        }

  
        public int GetPanelControlsMaxTop(int Order)
        {

            int Result = -1;
            int Height = 0;
            foreach (Control item in this.Controls)
            {
                if (item.Top > Result && ((GroupInfo)item.Tag).Order < Order)
                {
                    Result = item.Top;
                    Height = item.Height;
                }

            }
            if (Result == -1) Result = 1;
            return Result + Height;
        }


        public void Remove(Label DraggedLabel)
        {

        }
        public void Redraw()
        {
            List<Label> AllLabels = new List<Label>();
            foreach (Control item in this.Controls)
                if (item is Label)
                    AllLabels.Add((Label)item);

            int CurrentOrder = -1;
            Label NextLabel = null;
            foreach (Label item in AllLabels)
            {
                foreach (Label item2 in AllLabels)
                    if (NextLabel == null)
                    {
                        if (((GroupInfo)item2.Tag).Order > CurrentOrder)
                            NextLabel = item2;
                        else
                            continue;
                    }
                    else
                        if (((GroupInfo)item2.Tag).Order > CurrentOrder && ((GroupInfo)item2.Tag).Order < ((GroupInfo)NextLabel.Tag).Order)
                            NextLabel = item2;
                CurrentOrder = ((GroupInfo)NextLabel.Tag).Order;
                //NextLabel.Top = GetPanelControlsMaxTop(((GroupInfo)NextLabel.Tag).Order);
                NextLabel.Location = new System.Drawing.Point(1, GetPanelControlsMaxTop(((GroupInfo)NextLabel.Tag).Order) + 1);
                NextLabel.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                            | System.Windows.Forms.AnchorStyles.Right)));

                NextLabel = null;
            }
        }

        #region IDragSource Members

        public event DraggingHandler Dragging;
        public event DropTestHandler DropTest;

        void Mover_Dragging(Control Sender, Rectangle Area)
        {
            if (this.Dragging != null)
                this.Dragging(Sender, Area);
        }

        Control Mover_DropTest(Control Sender)
        {
            if (this.DropTest != null)
                return this.DropTest(Sender);
            return null;
        }

        public void DragCompleted(Control Sender)
        {
            ((IDraggableControl)Sender).Mover.Dragging -= Mover_Dragging;
            ((IDraggableControl)Sender).Mover.DropTest -= Mover_DropTest;
            this.Redraw();
        }

        #endregion

        #region IDragDestination Members
        private Color OriginalColor;
        public void DragOut()
        {
           //this.BorderStyle = BorderStyle.None;
            this.BackColor = OriginalColor;
        }

        public new void DragOver()
        {
            //OriginalColor = this.BackColor;
            //this.BorderStyle = BorderStyle.FixedSingle;
            this.BackColor = System.Drawing.SystemColors.ControlDarkDark;
        }

        public bool TestHitTarget(Control Sender, Rectangle Area)
        {
            //if label parent is current panel, return
            //if (pnlDest.Controls.Count > 0)
            //    return false;
            if (((IDraggableControl)Sender).Mover.ParentContainer== this)
                return false;

            const int Shift = 5;

            int TotalLeft = this.Left;
            int TotalTop = this.Top;
            //int TotalBottm = this.Bottom + pnlDest.Top + this.Parent.Top + this.Parent.Parent.Top;
            //int TotalRight = this.Right + pnlDest.Left + this.Parent.Left + this.Parent.Parent.Left;
            Rectangle pnldestRect = new Rectangle(TotalLeft + Shift, TotalTop + Shift, this.Width - Shift, this.Height - Shift);

            Point LeftTop = new Point(Area.Left, Area.Top);
            Point LeftBottom = new Point(Area.Left, Area.Bottom);
            Point RightTop = new Point(Area.Right, Area.Top);
            Point RightBottom = new Point(Area.Right, Area.Bottom);

            if (isPointInRect(LeftTop, pnldestRect)) return true;
            if (isPointInRect(LeftBottom, pnldestRect)) return true;
            if (isPointInRect(RightTop, pnldestRect)) return true;
            if (isPointInRect(RightBottom, pnldestRect)) return true;
            return false;
        }
        public bool isPointInRect(Point p, Rectangle rec)
        {
            if (p.X > rec.Left && p.X < rec.Right && p.Y > rec.Top && p.Y < rec.Bottom)
                return true;
            else
                return false;
        }
        public new Control DragDrop(Control DraggedLabel)
        {
            //DraggedLabel.Dock = DockStyle.None;
            this.Controls.Add(DraggedLabel);            
            ((IDraggableControl)DraggedLabel).Mover.Dragging += new DraggingHandler(Mover_Dragging);
            ((IDraggableControl)DraggedLabel).Mover.DropTest += new DropTestHandler(Mover_DropTest);
            ((GroupLabel)DraggedLabel).GroupInfo.isSelectedRow = false;
            ((GroupLabel)DraggedLabel).GroupInfo.SelectedRowOrder = 0;
            this.Redraw();
            return this;
        }

        #endregion
    }
}
